package com.codermn.day726;


import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jxau_scf
 * @date 2021/7/26 - 10:05
 */
public class StuWithStream {
    public static List<Student> DataInit(){
        List<Student> studentList = Arrays.asList(
                new Student("小明", 168, "男"),
                new Student("大明", 182, "男"),
                new Student("小白", 174, "男"),
                new Student("小黑", 186, "男"),
                new Student("小红", 156, "女"),
                new Student("小黄", 158, "女"),
                new Student("小青", 165, "女"),
                new Student("小紫", 172, "女")
//                new Student("a", 168, "0"),
//                new Student("b", 182, "0"),
//                new Student("c", 174, "0"),
//                new Student("d", 186, "0"),
//                new Student("e", 156, "1"),
//                new Student("f", 158, "1"),
//                new Student("g", 165, "1"),
//                new Student("h", 172, "1")
        );
        return studentList;
    }

    public static void main(String[] args) {
        List<Student> studentList = DataInit();
//        groupBy(studentList);
//        filter(studentList);
//        total(studentList);
//        MaxAndMin(studentList);
    }

    //最大值和最小值
    private static void MaxAndMin(List<Student> studentList) {
        int maxTall = studentList.stream()
                .mapToInt(Student::getTall)
                .max()
                .getAsInt();
        int minTall = studentList.stream().mapToInt(Student::getTall).min().getAsInt();
        System.out.println("maxTall:"+maxTall+"    "+"minTall:"+minTall);
    }

    //求和
    private static void total(List<Student> studentList) {
        int totalTall = studentList.stream()
                .mapToInt(Student::getTall)
                .sum();
        System.out.println("求和后"+totalTall);
    }

    //stream流实现分组
    private static void groupBy(List<Student> studentList) {
        Map<String, List<Student>> groupBy = studentList.stream()
                .collect(Collectors.groupingBy(Student::getSex));
        System.out.println("分组后"+groupBy);
    }

    //过滤
    private static void filter(List<Student> studentList){
        List<Student> filter = studentList.stream()
                .filter(student -> student.getTall() > 180)
                .collect(Collectors.toList());
        System.out.println("过滤后"+filter);
    }
}
